package com.bupt.godxian.charging.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bupt.godxian.charging.entity.Bill;
import com.bupt.godxian.charging.entity.ChargeRecord;
import com.bupt.godxian.charging.entity.ChargeRequest;
import com.bupt.godxian.charging.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper

public interface ChargeRecordMapper extends BaseMapper<ChargeRecordMapper> {

    @Insert("INSERT INTO charge_record (id, order_id, create_time, charged_amount, charged_time, begin_time, end_time, charging_cost, service_cost, total_cost, pile_id, user_id) VALUES (#{id}, #{order_id}, #{create_time}, #{charged_amount}, #{charged_time}, #{begin_time}, #{end_time}, #{charging_cost}, #{service_cost}, #{total_cost}, #{pile_id}, #{user_id})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ChargeRecord chargeRecord);

    @Update("UPDATE charge_record SET end_time = #{endTime} WHERE id = #{id}")
    void updateEndTime(@Param("id") Long id, @Param("endTime") String endTime);

    @Select("SELECT id, SUM(charging_cost) AS t_charging_cost, SUM(service_cost) AS t_service_cost, SUM(total_cost) AS t_total_cost " +
            "FROM charge_record WHERE pile_id = #{pileId} GROUP BY id limit 1")
    Map<String, Object> getChargerFee(@Param("pileId") Long pileId);

    @Select("SELECT * from charge_record WHERE user_id = #{userId}")
    List<ChargeRecord> findByUserId(@Param("userId") int userId);

//    @Select("SELECT c.order_id, c.charged_time, c.begin_time, r.request_submit_time, r.battery_size \" +\n" +
//            "                    \"FROM ChargedRecord c INNER JOIN ChargedRequest r ON c.order_id = r.order_id \" +\n" +
//            "                    \"ORDER BY c.order_id, r.request_submit_time, r.battery_size, c.charged_time, c.begin_time \" + \n" +
//            "                    \"WHERE user_id = #{userId}")
    @Select("SELECT c.order_id, c.charged_time, c.begin_time, r.request_submit_time, r.battery_size " +
            "FROM charge_record c INNER JOIN charge_request r ON c.user_id = r.user_id " +
            "WHERE c.user_id =  #{userId} " + // 添加一个空格
            "ORDER BY c.order_id, r.request_submit_time, r.battery_size, c.charged_time, c.begin_time")
    List<Bill> findBillByUserId(@Param("userId") int userId);

    @Select("SELECT COUNT(*) + 1 AS record_id FROM charge_record")
    Integer get_record_id();

}


